<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 标签ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */

quanli_require('module/base/goods/goodsTag.class.php');

class ajax_goodsTag {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品标签
                    $this->searchGoodsTag();
                    break;
			    case 2: //添加和修改商品标签
                    $this->saveGoodsTag();
                    break;
                case 4: //删除商品标签
                    $this->deleteGoodsTag();
                    break;
                case 5: //标签的使用状态
                    $this->statusGoodsTag();
                    break;
                case 6: //待编辑信息查询
                    $this->searchOneGoodsTag();
                    break;
                case 7: //最大序列号查询查询
                    $this->seachsort();
                    break;
                case 8:	//检查标签是否存在商品
                	$this->checkTag();
                	break;    
            }

        }

    }
    /**
     * 查询商品标签
     */
    private function searchGoodsTag() {

        global $G_SHOP;
        
		$tagHandler = new goodsTag( $G_SHOP->DBCA ) ;
		
		$result_data  = $tagHandler->goodsTagGroupList( $_REQUEST ) ;

		$result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {

			$edit_permission = $G_SHOP->has_permission(512);
            foreach($result_data as &$s_l) {
            	
            	if($edit_permission){
	        		$result_array['rows'][] = array(
	                    'id'   => $s_l['tagGroupID'],
	                    'cell' => array(
                			'<a href="javascript: void(0);" onclick="goodsTag.edit(' . $s_l['tagGroupID'] . ')">维护值</a>&nbsp;' ,
                			$s_l['tagGroupName'],
                			$s_l['tagVal'],
                			$s_l['sortOrder'],
                			$s_l['useFlg'] == 1 ?
                			'<a href="javascript: void(0);" onclick="goodsTag.doAction(' . $s_l['tagGroupID'] . ', 0) " >启用</a>' :
                			'<a href="javascript: void(0);" onclick="goodsTag.doAction(' . $s_l['tagGroupID'] . ', 1 ) " >禁用</a>'
						)
	                    
	                );
            	}else{
            		$result_array['rows'][] = array(
	                    'id'   => $s_l['tagGroupID'],
	                    'cell' => array(
	            			$s_l['tagGroupID'] ,
	            			$s_l['tagGroupName'],
	            			$s_l['tagVal'],
	            			$s_l['sortOrder'],
	            			$s_l['useFlg'] == 1 ?
	            			'启用' :
	            			'禁用'
	                    )
	                );
            	}
            }
        }

        echo json_encode( $result_array );
    }
    
    /**
     * 标签保存和更新
     *
     */
    private function saveGoodsTag () {
    	global $G_SHOP;
    	
    	$_REQUEST['creator'] = $G_SHOP->currentUserID;
    	$tagHandler = new goodsTag( $G_SHOP->DBCA ) ;
    	$result_array = $tagHandler->saveGoodsTag( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
    
	/**
	 * 删除标签
	 *
	 */
	private function deleteGoodsTag () {
		global $G_SHOP;
		
    	$tagHandler = new goodsTag( $G_SHOP->DBCA ) ;
    	$result_array = $tagHandler->deleteTagGroup( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	/**
	 * 修改标签的使用状态
	 *
	 */
	private function statusGoodsTag () {  
		global $G_SHOP;
		
    	$tagHandler = new goodsTag( $G_SHOP->DBCA ) ;
    	$result_array = $tagHandler->changeUse( $_REQUEST ) ;
    	echo json_encode($result_array);
	}

    /*
     * 待编辑信息chax
     *
     * */
    private  function searchOneGoodsTag(){

        global $G_SHOP;

        $arg_array = array('tagGroupID' => $_REQUEST['tagID']);
        $tagHandler = new goodsTag( $G_SHOP->DBCA ) ;
        $result_data  = $tagHandler->goodsTagGroupList( $arg_array ) ;

        $result = array(
            'tagID'	  => $result_data[0]['tagGroupID'],
            'tagName'	  => $result_data[0]['tagGroupName'],
            'memoName'	  => $result_data[0]['memoName'],
            'sortOrder'	  => $result_data[0]['sortOrder'],
            'useFlg'	  => $result_data[0]['useFlg'],
            'tagArray'   => $result_data[0]['tagArray'],
        );

        echo json_encode($result);
    }

   //最大序列号查询
    private function seachsort(){

        global $G_SHOP;
        
        $tagHandler = new goodsTag( $G_SHOP->DBCA ) ;
        
        $sortOrder = $tagHandler->maxSortOrder();

        echo json_encode(array('sortOrder' => $sortOrder));
    }
    
    /**
     * 检查标签是否存在商品
     *
     */
    private function checkTag(){
    	global $G_SHOP;

    	$check_tag = $G_SHOP->DBCA->stmt_query('SELECT * FROM goods_tag_relation WHERE tagGroupID = ? AND tagID = ?', 'ii', $_REQUEST['tagGroupID'], $_REQUEST['tagID']);
    	
    	if ($check_tag) {
    		echo json_encode(array('code' => 400, 'message' => '当前标签值存在商品，不能删除'));
    	} else {
    		echo json_encode(array('code' => 200, 'message' => '可以删除'));
    	}
    }
}